<?php
class Game {
    const HAS_MONEY = 1;    //有钱
    const No_Money  = 0;    //没钱
    const SOLD_OUT  = 2;    //售完
    const SOLD      = 3;   //售货中

    public $state = Game::No_Money; //默认状态为 售完
    public $count = 10;      //糖果数量

    public function setState($state)
    {
        $this->state = $state;
    }
    //投入钱
    public function insertMoney($money)
    {
        if ($this->state == Game::HAS_MONEY)
        {
            echo "游戏暂时还没结束，请等待";
        } elseif ($this->state == Game::SOLD)
        {
            echo "游戏暂时还没结束，请稍等";
        } elseif ($this->state == Game::SOLD_OUT && $this->count == 0)
        {
            $this->state = Game::HAS_MONEY;
            //售完就退款
            $this->reject();
            echo "已售完, 游戏无法开始";
        }elseif ($this->state == Game::SOLD_OUT && $this->count != 0){
            $this->state = Game::HAS_MONEY;
            echo "请开始游戏";
        }elseif ($this->state == Game::No_Money)
        {
            if ($money == '25')
            {
                echo "请开始游戏";
                $this->state = Game::HAS_MONEY;
            } else {
                echo "请投25分钱";
                $this->reject();
            }

        }
    }

    //转动开始游戏
    public function trun() {
        if ($this->state == Game::HAS_MONEY)
        {
            $this->state = Game::SOLD;
            echo "正在抽取糖果中";
        } elseif ($this->state == Game::SOLD)
        {
            echo "在售货中，请勿动";
        } elseif ($this->state == Game::SOLD_OUT && $this->count == 0)
        {
            $this->reject();
            echo "已售完, 游戏无法开始";
        }elseif ($this->state == Game::No_Money)
        {
            echo "投币";
        }
    }

    //获取糖果
    public function get()
    {
        if ($this->state == Game::HAS_MONEY)
        {
            echo "请扭动转盘";
        } elseif ($this->state == Game::SOLD)
        {
            $this->state = Game::SOLD_OUT;
            $this->count -= 10;
            echo '请您获取糖果';
        } elseif ($this->state == Game::SOLD_OUT && $this->count == 0)
        {
            echo "已售完, 游戏无法开始";
            $this->reject();
        }elseif ($this->state == Game::No_Money)
        {
            echo "投币";
        }
    }

    //退款
    public function reject()
    {
        if ($this->state == Game::HAS_MONEY)
        {
            echo "请获取您的金币";
            $this->state = Game::SOLD_OUT;
        } elseif ($this->state == Game::SOLD)
        {
            echo '无法退回，糖果正在摇动中';
        } elseif ($this->state == Game::SOLD_OUT)
        {
            echo "无法退回，糖果已被摇出";
            $this->state = Game::No_Money;
        }elseif ($this->state == Game::No_Money)
        {
            echo "请投币";
        }
    }
}